<?php ob_start();
session_start(); //on relaye la session
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
	$enregistrer = $_SESSION["is_registered"];
	$message="";
}else{
	header("Location:../../menu.php?erreur=intru"); // redirection en cas d'echec
}
// script PHP interrogation Base de donnees pour reponse a la requette AJAX
require_once('../../Connections/microassur.php');
require_once('../../utils/fonctions.php');

include ("config.php");
$_SESSION['formule']=$_POST['val_sel'];
if(!isset($_SESSION['MM_Username'])) $_SESSION['MM_Username'] = -1;
$idunite=-1;
$Identifiant_user=$_SESSION["iduser"];
function getCodeUser($Identifiant_user) {
	 $sql="select Code_utilisateur from utilisateur where Identifiant_utilisateur='$Identifiant_user'";
	$res = mysql_query($sql);
	$row = mysql_fetch_object($res);
	return $row->Code_utilisateur;
}
// Connexion a la base de donnees  
	  mysql_select_db($database_microassur, $microassur);
		$code = getCodeUser($Identifiant_user);
	   $sql="select Identifiant_unite from agent where code_agent='$code'";
	  $req = mysql_query($sql);
	 $rep=mysql_fetch_array($req); $n=mysql_num_rows($req);
	 if($n!=0)  $idunite=$rep['Identifiant_unite'];
	// 
	  $date=explode('-',$_SESSION['dateeffet']); // $date=explode('/',$_SESSION['dateeffet']); //louis (bug)
	  $j=$date[0];
	  $m=$date[1];
	  $a=$date[2];
	 // $date=date("Y-m-d", mktime(0, 0, 0, $m+12, $j-1,  $a));
	  $datefin=ajoutDate($_SESSION['dateeffet'],12,-1,0);
	  $requete=mysql_query("select * from contrat where Numero_contrat='".$_SESSION['contrat']."'")or die(mysql_error());
	 if($requete)$num=mysql_fetch_array($requete);
	 $bool=true;
	if($num==0) 
	{
		mysql_query("START TRANSACTION"); $bool=mysql_query("insert into contrat values('','".$_SESSION['contrat']."','A','".dateEN2FR($_SESSION['dateeffet'])."','".dateEN2FR($datefin)."','A','','','','$idunite','')") or die(mysql_error());
		if(!$bool)
		{
			mysql_query("rollback");header("location:index.php?ajout=echec");
		}
		else
		{
			mysql_query("commit");
		}
			
	}
	else
	{
		$req=mysql_query("select max(Identifiant_contrat) as idcontrat from contrat");
		$rep=mysql_fetch_array($req);
		$_SESSION['idcontrat']=$rep['idcontrat'];
		$_SESSION['datefin']=$date;
	}
	 $nb=0;
	 $var="";
	$QuestionBase = "SELECT * from conditionvente  WHERE (Identifiant_offre='".$_GET['id2']."'  or Identifiant_produit='".$_GET['id1']."') and (Type_condition_vente='C') ORDER BY Identifiant_condition_vente ASC " ;
	$result_recherche=mysql_query($QuestionBase, $microassur) or die (mysql_error());
	if($result_recherche)$nb=mysql_num_rows($result_recherche);
// construction de la liste deroulante
$aff="";
 if($nb>0)$aff.="<span><input value='B' type='checkbox' id='beneficaire'/><span>BENEFICIAIRE</span></span>"; 
// envoi reponse Php a Ajax	
	echo $aff;
?>
<?php ob_end_flush();?>